package com.tally.user.controller;

import com.tally.server.dto.ExpDTO;
import com.tally.server.dto.ResponseDTO;
import com.tally.server.dto.SysExpDetailDTO;
import com.tally.server.service.ExpService;
import com.tally.server.service.SysExpDetailService;
import com.tally.server.service.SysLevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: sy
 * @Date: 2021/1/29 18:08
 * @Description:
 */
@RestController
@RequestMapping("/exp")
public class ExpController {
    public static final String BUSINESS_NAME = "经验相关";

    @Autowired
    private ExpService expService;

    @Autowired
    private SysLevelService levelService;

    @Autowired
    private SysExpDetailService expDetailService;

    @GetMapping("/get-user-exp/{id}")
    public ResponseDTO getUserExp(@PathVariable String id){
        if(StringUtils.isEmpty(id)){
            return ResponseDTO.success(null);
        }
        ExpDTO expDTO = expService.selectByUserId(Integer.parseInt(id));
        if(expDTO == null){
            expDTO = new ExpDTO();
            expDTO.setExp(0);
            expDTO.setUserId(Integer.parseInt(id));
            expDTO.setLevel(1);
            expDTO.setLastMaxExp(0);
            expDTO.setMaxExp(levelService.getMaxExp(1));
            return ResponseDTO.success(expDTO);
        }
        Integer level = levelService.getLevel(expDTO);
        Integer maxExp = levelService.getMaxExp(level);
        Integer lastMaxExp = levelService.getMaxExp(level - 1);
        expDTO.setLevel(level);
        expDTO.setMaxExp(maxExp);
        expDTO.setLastMaxExp(lastMaxExp);
        return ResponseDTO.success(expDTO);
    }

    @GetMapping("/sys/load-exp-detail")
    public ResponseDTO loadExpDetail(){
        List<SysExpDetailDTO> sysExpDetailDTOList = expDetailService.selectAll();
        if(CollectionUtils.isEmpty(sysExpDetailDTOList)){
            return ResponseDTO.fail("无经验详情记录");
        }
        return ResponseDTO.success(sysExpDetailDTOList);
    }
}
